package cn.myspringboot.system.controller;

import cn.myspringboot.anno.PreAuthorize;
import cn.myspringboot.system.service.EmployeeService;
import cn.myspringboot.user.domain.vo.EmployeeVo;
import cn.myspringboot.user.query.EmployeeQuery;
import cn.myspringboot.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api("员工相关的接口")
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PreAuthorize(name = "添加或修改员工信息",sn = "employee:insert",descs ="添加或修改员工信息")
    @ApiOperation("添加或修改员工信息")
    @PostMapping
    public R insert(@RequestBody EmployeeVo employeeVo) {
        if(employeeVo.getId() == null){
            employeeService.add(employeeVo);
        }else {
            employeeService.update(employeeVo);
        }
        return R.ok();
    }
    @PreAuthorize(name = "根据id删除员工信息",sn = "employee:delete",descs ="根据id删除员工信息")
    @ApiOperation("根据id删除员工信息")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        employeeService.delete(id);
        return R.ok();
    }
    @PreAuthorize(name = "批量删除员工信息",sn = "employee:delBatch",descs ="批量删除员工信息")
    @ApiOperation("批量删除员工信息")
    @PostMapping("/delBatch")
    public R delBatch(@RequestBody List<Long> ids) {
        employeeService.batchDelete(ids);
        return R.ok();
    }
    @PreAuthorize(name = "根据id查询员工信息",sn = "employee:getById",descs ="根据id查询员工信息")
    @ApiOperation("根据id查询员工信息")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(employeeService.getById(id));
    }
    @PreAuthorize(name = "查询所有员工信息",sn = "employee:getAll",descs ="查询所有员工信息")
    @ApiOperation("查询所有员工信息")
    @GetMapping("/getAll")
    public R getAll() {
        return R.ok(employeeService.getAll());
    }
    @PreAuthorize(name = "分页查询员工信息",sn = "employee:page",descs ="分页查询员工信息")
    @ApiOperation("分页查询员工信息")
    @PostMapping("/page")
    public R page(@RequestBody EmployeeQuery employeeQuery) {
        return R.ok(employeeService.selectEmployeePage(employeeQuery));
    }
    
}
